var Skink; if (!Skink) Skink = {};

/***********
Skink.Prefs
************/
if (!Skink.Windows) Skink.Windows = {};

Skink.Windows.windowExitCalled = false;

Skink.Windows.onWindowActive = function(event) {
    Skink.dump('Window ACTIVE');
    if ($('body').focus()) {}
}

Skink.Windows.onWindowDeactivate = function(event) {
    Skink.UI.hideTooltips();
};

Skink.Windows.windowMinimize = function() {
    window.nativeWindow.minimize();
    if (Skink.Prefs.get('window-minimizetosystray') && air.NativeApplication.supportsSystemTrayIcon) {
        window.nativeWindow.visible = false;
    }
    return false;
};

Skink.Windows.windowRestore = function() {
    Skink.dump('restoring window');
    Skink.dump('current window state:' + window.nativeWindow.displayState);
    Skink.dump('restoring window');
    window.nativeWindow.restore();

    Skink.dump('activating window');
    window.nativeWindow.activate();
    if (air.NativeApplication) {
        Skink.dump('activating application');
        air.NativeApplication.nativeApplication.activate();
    }
};

Skink.Windows.onAppExit = function(event) {
    air.trace('Skink.Windows.windowExitCalled is ' + Skink.Windows.windowExitCalled);
    $('body').fadeOut(500);
    Skink.Prefs.savePrefs();

    if (event) {
        air.trace('onAppExit triggered by event')
    }

    window.nativeWindow.removeEventListener(air.Event.CLOSING, Skink.Windows.onWindowClose);
    window.nativeWindow.removeEventListener(air.Event.EXITING, Skink.Windows.windowClose);
	air.NativeApplication.nativeApplication.exit();
}

Skink.Windows.onWindowClose = function(event) {
    Skink.Prefs.savePrefs();
};

/**
* Called when the user closes the main window.
*/
Skink.Windows.windowClose = function() {
    Skink.Prefs.savePrefs();
    Skink.Windows.windowExitCalled = true;
    Skink.Windows.onAppExit();
};

Skink.Windows.makeSystrayIcon = function() {
    if (air.NativeApplication.supportsSystemTrayIcon) { // system tray on windows
        Skink.dump('Making Windows system tray menu')
        air.NativeApplication.nativeApplication.icon.tooltip = "Skink hates your guts";
        var systrayIconLoader = new air.Loader();
        systrayIconLoader.contentLoaderInfo.addEventListener(air.Event.COMPLETE, Skink.Menus.iconLoadComplete);
        systrayIconLoader.load(new air.URLRequest("images/skink-icon-alpha_16.png"));
        air.NativeApplication.nativeApplication.icon.addEventListener('click', Skink.Windows.onSystrayClick);
    }
};

Skink.Windows.onSystrayClick = function(event) {
    Skink.Windows.windowRestore();
}

Skink.Windows.openHTMLUtilityWindow = function(url) {
    var options = new air.NativeWindowInitOptions();
    options.systemChrome = air.NativeWindowSystemChrome.STANDARD;
    options.type = air.NativeWindowType.UTILITY;

    var windowBounds = new air.Rectangle(200, 250, 300, 400);
    var newWindow = air.HTMLLoader.createRootWindow(true, options, true, windowBounds);
    newWindow.load(new runtime.flash.net.URLRequest(url));
}

Skink.Windows.makeWindowVisible = function() {
    window.nativeWindow.visible = true;
}

Skink.Windows.makeWindowHidden = function() {
    window.nativeWindow.visible = false;
}

Skink.Windows.setWindowOpacity = function(percentage) {
    var val = parseInt(percentage) / 100;
    window.htmlLoader.alpha = val;
}

Skink.Windows.windowMove = function() {
    nativeWindow.startMove();
}

Skink.Windows.windowResize = function() {
    nativeWindow.startResize(air.NativeWindowResize.BOTTOM_RIGHT);
}

Skink.Windows.resetPosition = function() {
    nativeWindow.x = Skink.Prefs.defaultPreferences['window-x'];
    nativeWindow.y = Skink.Prefs.defaultPreferences['window-y'];
    air.trace(Skink.Prefs.defaultPreferences['window-x'] + "x" + Skink.Prefs.defaultPreferences['window-y']);
    air.trace(nativeWindow.x + "x" + nativeWindow.y);
    Skink.Windows.onWindowMove();

    nativeWindow.width = Skink.Prefs.defaultPreferences['window-width'];
    nativeWindow.height = Skink.Prefs.defaultPreferences['window-height'];
    air.trace(Skink.Prefs.defaultPreferences['window-width'] + "x" + Skink.Prefs.defaultPreferences['window-height']);
    air.trace(nativeWindow.width + "x" + nativeWindow.height);
    Skink.Windows.onWindowResize();
}

Skink.Windows.onWindowResize = function() {
    Skink.Prefs.set('window-width', nativeWindow.width);
    Skink.Prefs.set('window-height', nativeWindow.height);
};

Skink.Windows.onWindowMove = function() {
    Skink.Prefs.set('window-x', nativeWindow.x);
    Skink.Prefs.set('window-y', nativeWindow.y);
};